package com.boo.service;

import java.util.ArrayList;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import vn.com.hoasua.database.smng.LinkMng;
import vn.com.hoasua.database.sobject.LinkPost;
import android.app.Activity;
import android.content.Context;

import com.hoasua.database.R;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;

/**
 * Coolio base service - all the service class must be override this class 
 * 
 * @author tuyendn <tuyendn@vnext.vn>
 * @since 2013/12/27
 * */
public abstract class BaseService {

	private static final String ACCEPT_HEADER   = "application/json";
	private static final String CONTENT_TYPE    = "application/json";
	private static final String ACCEPT_ENCODING = "gzip";
	
	protected AsyncHttpClient client;
	protected Activity parentActivity;
	
	/**
	 * Default constructor 
	 * 
	 * @author thuanhm <minhthuan2602@gmail.com>
	 * @since 2013/12/27
	 * @param <Activity> parentActivity - The parent activity that call the service 
	 * */
	public BaseService(Activity parentActivity) {
		this.client = new AsyncHttpClient();
		//this.client.addHeader("Accept", "application/json");
		//this.client.addHeader("Content-type", "application/json");
		//this.client.addHeader("Accept-Encoding", "gzip");
		this.parentActivity = parentActivity;
	}
	
	/**
	 * Default detructor, clear all the memory
	 * 
	 * @author  thuanhm <minhthuan2602@gmail.com>
	 * @since 2013/12/27
	 * */
	public void finalize ()  {
		this.client = null;
		this.parentActivity = null;
	}
	
	
	/**
	 * Set the parent activity 
	 * 
	 * @author thuanhm <minhthuan2602@gmail.com>
	 * @since 2013/12/27 
	 * @param <Activity> parentActivity - The parent activity that call the service 
	 * */
	public void setParentActivity(Activity parentActivity) {
		this.parentActivity = parentActivity;
	}
	
	/**
	 * Get the parent activity 
	 * 
	 * @author  thuanhm <minhthuan2602@gmail.com>  
	 * @since 2013/12/27
	 * */
	public Activity getParentActivity() {
		return this.parentActivity;
	}
	
	/**
	 * Post a request to server using url service 
	 * 
	 * @author  thuanhm <minhthuan2602@gmail.com> 
	 * @since 2013/12/27
	 * 
	 * @param <String> url - url service
	 * @param <StringEntity> entity - add parameter to request 
	 * @param <AsyncHttpResponseHandler> handler - callback handler class string 
	 * */
	
	public void post(String url, StringEntity entity, AsyncHttpResponseHandler handler) {
		if (this.client != null) {
			
			this.client.post(this.parentActivity, url, entity, CONTENT_TYPE, handler);
		}
	}
	
	public void post(String url,AsyncHttpResponseHandler handler) {
		if (this.client != null) {
			
			this.client.post(this.parentActivity, url, null, CONTENT_TYPE, handler);
		}
	}
	
	public void get (Context context, String url, JsonHttpResponseHandler handler){
		LinkMng linkMng = new LinkMng(context);
		ArrayList<LinkPost> cache = linkMng.select(LinkMng.link, url);
		if(cache==null || cache.isEmpty()){
			// do nothing
		}else{
			// return cache data
			long timeOut = Long.valueOf(context.getString(R.string.time_out));
			long timeStamp = cache.get(0).time;
			if(System.currentTimeMillis()-timeStamp <= timeOut){
				String json = cache.get(0).xml;
				try {
					JSONObject jsonObj = new JSONObject(json);
					handler.onSuccess(jsonObj);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		if(this.client!=null){
			this.client.get(context, url, handler);
			//this.client.get(url, entity, handler);
		}
	}
	
	public void setCache(String url,String json){
		LinkMng linkMng = new LinkMng(parentActivity);
		LinkPost link = new LinkPost(-1, new String[LinkPost.NUM_FIELDS]);
		link.link = url;
		link.xml= json;
		link.time=System.currentTimeMillis();
		ArrayList<LinkPost> lstLink = linkMng.select(LinkMng.link, url);
		if(lstLink==null || lstLink.isEmpty()){
			linkMng.insertNewTo(link.getValues());
		}else{
			linkMng.insertTo(lstLink.get(0)._id, link.getValues());
		}
	}
	
	public void post (String url, StringEntity entity){
		if (this.client != null) {
			this.client.post(this.parentActivity, url, entity, CONTENT_TYPE,null);
		}
	}
	
	
	
	/**
	 * Post a request to server using url service 
	 * 
	 * @author  thuanhm <minhthuan2602@gmail.com> 
	 * @since 2013/12/27
	 * 
	 * @param <String> url - url service
	 * @param <StringEntity> entity - add parameter to request 
	 * @param <JsonHttpResponseHandler> handler - callback handler class json 
	 * */
	public void post(String url, StringEntity entity, JsonHttpResponseHandler handler) {
		if (this.client != null) {
			this.client.post(this.parentActivity, url, entity, CONTENT_TYPE, handler);
		}
	}
}
